package com.expressage.controller;

import com.alibaba.fastjson.JSONArray;
import com.expressage.po.AreaAddress;
import com.expressage.po.CityAddress;
import com.expressage.po.StreetAddress;
import com.expressage.service.AreaAddressService;
import com.expressage.service.CityAddressService;
import com.expressage.service.StreetAddressService;
import com.expressage.vo.AdderVO;
import com.expressage.vo.AreaAddressVO;
import com.expressage.vo.StreetAddressVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 区控制器
 */
@Controller
@RequestMapping("/jspafter")
public class StreetAddressController {

    Logger logger=Logger.getLogger(StreetAddressController.class);
    @Autowired
    AreaAddressService areaAddressService;
    @Autowired
    CityAddressService cityAddressService;
    @Autowired
    StreetAddressService streetAddressService;
    @RequestMapping("/findallstreet")
    public String findAllArea(Model model){
        List<StreetAddressVO> list=new ArrayList<>();
        list=streetAddressService.findAll();
        for (StreetAddressVO s:list
             ) {
            System.out.println(s.getAreaname()+"\t"+s.getStreetname());
        }
        model.addAttribute("list",list);
        return "jspafter/streetlist";
    }
    @RequestMapping(value="/jfindbyareaid",produces={"text/html;charset=UTF-8;","application/json;"})
    public @ResponseBody
    String JfindByAreaid(int id){
        System.out.println("id"+id);
        List<StreetAddressVO> list=new ArrayList<StreetAddressVO>();
        list=streetAddressService.findByAreaid(id);
        for (StreetAddressVO s: list
             ) {
            System.out.println(s.getStreetid()+s.getStreetname());
        }
        return JSONArray.toJSONString(list);
    }
    /*
    @RequestMapping("/findbyareaid")
    public String findByAreaid(int id,Model model){
        AreaAddressVO areaAddress=areaAddressService.findByAreaid(id);
        model.addAttribute("areaAddress",areaAddress);
        return "";
    }
    //未完成功能*/
    @RequestMapping("/toinsertstreet")
    public String toAddArea(Model model){

        List<AreaAddressVO> list1=new ArrayList<>();
        list1=areaAddressService.findAll();
        model.addAttribute("list1",list1);
        List<CityAddress> list2=new ArrayList<>();
        list2=cityAddressService.findAll();
        model.addAttribute("list2",list2);

        return "jspafter/addstreet" ;
    }
    @RequestMapping("/insertstreet")
    public String addStreet(AdderVO adderVO){
        String areaName= adderVO.getAreaName();
        String streetName=adderVO.getStreetName();
        System.out.println("streetName"+streetName);
        System.out.println("areaName"+areaName);
        AreaAddressVO areaAddressVO= areaAddressService.findByAreaName(areaName);
        System.out.println("duixiang "+areaAddressVO);
        int areaid= areaAddressVO.getAreaid();
        StreetAddressVO streetAddress=new StreetAddressVO();
        streetAddress.setAreaid(areaid);
        streetAddress.setStreetname(adderVO.getStreetName());
        boolean flag=streetAddressService.insert(streetAddress);
        if(true){
            return  "redirect:findallstreet.action";
        }else {
            return "";
        }
    }
    @RequestMapping("/deletestreet")
    public String deletstreet(int[] id){

        int id1=id[0];

        StreetAddressVO streetAddressVO= streetAddressService.findByStreetid(id1);

        streetAddressVO.setStreetname("删除状态");

        boolean flag=streetAddressService.update(streetAddressVO);
        if(flag){
            return  "redirect:findallstreet.action";
        }else {
            return "";
        }


    }
    @RequestMapping("/toupdatestreet")
    public String toupdateStreet(int id,Model model){
        /*AreaAddressVO areaAddress=areaAddressService.findByAreaid(id);*/
        StreetAddressVO streetAddressVO=streetAddressService.findByStreetid(id);
        model.addAttribute("streetAddressVO",streetAddressVO);
        return "jspafter/updatestreet";
    }

    @RequestMapping("/updatestreet")
    public String updateArea(StreetAddressVO streetAddressVO){


        boolean flag=streetAddressService.update(streetAddressVO);
        if(flag){
            return  "redirect:findallstreet.action";
        }else {
            return "";
        }
    }

}
